// pages/orderDetails/index.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderStatusMap: {
      "1": "待支付",
      "2": "已支付",
      "3": "已完成",
      "-1": "已取消",
    },
    info: {},
    orderStatus: null,
    orderId: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option: any) {
    // 初始化去调用订单详情接口
    console.log(option, "option");
    this.setData(
      {
        orderId: option.orderId || 0,
      },
      () => {
        this.queryOrderDetail();
      }
    );
  },
  async queryOrderDetail() {
    try {
      const res = await getApp().request({
        path: "/s/o/getOrder",
        data: {
          id: this.data.orderId,
        },
      });
      console.log(res, "rsss");
      this.setData({
        info: res,
        orderStatus: res.status.toString(),
      });
    } catch {}
  },
  async handlePay() {
    try {
      const res = await getApp().request({
        path: "/s/pr/oorderPp",
        method: "POST",
        data: {
          orderId: this.data.orderId,
        },
      });
      console.log(res, "resss");
      const { timeStamp, nonceStr, packages, signType, paySign } = res;
      wx.requestPayment({
        timeStamp,
        nonceStr,
        package: packages,
        signType,
        paySign,
        success: (res) => {
          if (res.errMsg === "requestPayment:ok") this.queryOrderDetail();
        },
        fail(res) {},
      });
    } catch {}
  },
  async handleCencal() {
    try {
      await getApp().request({
        path: "/s/o/concelOrder",
        method: "POST",
        data: {
          id: this.data.orderId,
        },
      });
      wx.showToast({
        title: "订单取消成功",
        icon: "success",
      });
    } catch {}
  },
  handleBack() {
    wx.navigateBack(1);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
